/*
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Topless JSF.
 *
 * The Initial Developer of the Original Code is
 * Marco Antonio Villalobos, Jr. (mvillalobos@kineteque.com).
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Marco Antonio Villalobos, Jr. (mvillalobos@kineteque.com).
 *
 * ***** END LICENSE BLOCK *****
 */

package kineteque.jsf.topless.managedbeans;

import org.apache.derby.jdbc.EmbeddedDataSource;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.io.Serializable;
import java.sql.*;
import java.security.SecureRandom;

@ManagedBean(name = "appConfig", eager = true)
@ApplicationScoped
public class ApplicationConfig implements Serializable {

    protected DataSource dataSource = null;

    @PostConstruct
    public void init() {
        try {
            dataSource = createDataSource();
            int size = 10000;
            Connection con = dataSource.getConnection();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT COUNT(*) from SYS.SYSTABLES where TABLENAME = 'WIDGETS'");
            rs.next();
            boolean drop = (1==rs.getInt(1));
            if (drop) {
                stmt.execute("DROP TABLE widgets");
            }
            stmt.execute("CREATE TABLE widgets ( widget_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, name VARCHAR(50), description VARCHAR(100), active SMALLINT NOT NULL DEFAULT 1, price FLOAT NOT NULL DEFAULT 0, create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)");
            PreparedStatement pstmt = con.prepareStatement("INSERT INTO widgets(name, description, active, price) VALUES (? , ?, ?, ?)");
            for (int i=0;i<size;i++) {
                pstmt.setString(1, "Name "+(i+1));
                pstmt.setString(2, "Description "+(i+1));
                pstmt.setBoolean(3, i % 2 == 0);
                pstmt.setFloat(4, new SecureRandom().nextFloat()*1000f);
                pstmt.executeUpdate();
            }
            stmt.close();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    protected DataSource createDataSource() {
        EmbeddedDataSource ds = new EmbeddedDataSource();
        ds.setDatabaseName("target/topless-db");
        ds.setCreateDatabase("create");
        return ds;
    }

    public DataSource getDataSource() {
        return dataSource;
    }
}